package com.jixiantubu.wode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.YoujishoucangAdapter;
import com.jixiantubu.bean.Collectbean;
import com.jixiantubu.bean.Travelsbean;
import com.jixiantubu.jinnang.JinNang_YouJi_JuTiNeiRong;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class YoujishoucangActivity extends Activity {

	ListView youjishoucang_lv;
	// 数据来源
	List<Travelsbean> list = new ArrayList<Travelsbean>();
	HttpUtils httpUtils = new HttpUtils();
	YoujishoucangAdapter adapter;
	MyApplication application;
	ImageView shezhifanhui;
	Collectbean collectbean;

	Intent intent;
	String username;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_youjishoucang);
		intent = getIntent();
		username = intent.getStringExtra("username");
		Log.e("YoujishoucangActivity------->", username);		
		initViews();
		initData();
	}

	private void initViews() {
		shezhifanhui = (ImageView) findViewById(R.id.wo_shezhi);
		youjishoucang_lv = (ListView) findViewById(R.id.youjishoucang_lv);
		youjishoucang_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(YoujishoucangActivity.this,
						JinNang_YouJi_JuTiNeiRong.class);
				intent.putExtra("neirong", list.get(arg2));
				startActivity(intent);
			}

		});

	}

	private void initData() {
		adapter = new YoujishoucangAdapter(YoujishoucangActivity.this, list);
		youjishoucang_lv.setAdapter(adapter);
		String url = "http://" + MyApplication.getIP()
				+ ":8080/Jinnang/WoGuanzhu";
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", "8");
		params.addBodyParameter("username", username);

		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// 获取服务器端返回的数据结果

						String result = arg0.result;
						Log.e("接收到的数据", result.toString());
						// shiyongGson框架进行json解析
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<Travelsbean>>() {
						}.getType();
						List<Travelsbean> list1 = gson
								.fromJson(result, typeOfT);
						list.addAll(list1);
						Log.e("hua-eee-----", list.toString());
						adapter.notifyDataSetChanged();
					}
				});

	}

	public void fanhui(View v) {
		finish();
	}

}